const BASE_AI_URL = 'https://openrouter.ai/api/v1/chat/completions';
const SITE_URL = 'rss.leeapp.dev';
const SITE_NAME = 'workers-rss';
export const prompt = `请返回您反复阅读正文后精心写成的详尽总结，并用中文撰写。 由于该内容为技术性周报文章，注意不要忽略技术细节, 如产品介绍， 版本更新， 新特性或功能提升等, 但请去除广告相关的内容。输出格式: Markdown 格式，包括 #标题、##H2、 ###H3、 加号符点、 子符点、 列表等。 以下是文章内容:`;

export async function summarize(text: string, API_KEY?: string) {
	try {
		const content = `${prompt}\n${text}`;
		const completion = await fetch(BASE_AI_URL, {
			method: 'POST',
			headers: {
				'Authorization': `Bearer ${API_KEY}`,
				'HTTP-Referer': `${SITE_URL}`,
				'X-Title': `${SITE_NAME}`,
				'Content-Type': 'application/json',
			},
			body: JSON.stringify({
				model: 'google/gemini-flash-1.5',
				messages: [
					{
						role: 'user',
						content,
					},
				],
			}),
		}).then(res => res.json() as any);
		console.log('completion:', completion);
		if (completion.error) return text;
		return completion.choices[0].message.content;
	} catch (err: any) {
		console.log(err);
		return text;
	}
}

export async function chat(content: string, API_KEY?: string) {
	try {
		const completion = await fetch(BASE_AI_URL, {
			method: 'POST',
			headers: {
				'Authorization': `Bearer ${API_KEY}`,
				'HTTP-Referer': `${SITE_URL}`,
				'X-Title': `${SITE_NAME}`,
				'Content-Type': 'application/json',
			},
			body: JSON.stringify({
				model: 'deepseek/deepseek-chat',
				messages: [
					{
						role: 'user',
						content,
					},
				],
			}),
		}).then(res => res.json() as any);
		console.log('completion:', completion);
		if (completion.error) return '';
		return completion.choices[0].message.content;
	} catch (err: any) {
		console.log(err);
		return '';
	}
}
